package com.hongxin.nw.netty.utils.pools;

import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 通道池,存放所有通道对象(通信连接)
 */
@Slf4j
public class ChannelPools {

    //设备编号 对应 通道
    private final static Map<String, ChannelHandlerContext> registryMap = new ConcurrentHashMap<>();

    public static void addCtx(final String code, final ChannelHandlerContext ctx) {
        registryMap.put(code, ctx);

    }
    public static int getSize(){
        return registryMap.size();
    }
    public static ChannelHandlerContext getCtx(final String code) throws Exception {
        if (registryMap.containsKey(code)){
            return registryMap.get(code);
        }else{
            throw new Exception("设备找不到: " + code);
        }
    }
    public static void removeCtx(final String code) {
        if (registryMap.containsKey(code)){
            registryMap.remove(code);
        }
    }
}